package com.hex.ds.hdtp.core.inf.common.utils;


import cn.hutool.core.collection.CollectionUtil;
import com.hex.ds.hdtp.core.inf.common.enums.SourceJdbcErrorCode;
import com.hex.ds.hdtp.core.inf.common.exception.SourceSystemException;
import lombok.experimental.UtilityClass;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import sun.security.krb5.KrbException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统安全认证工具类
 * @Package: com.hex.ds.source.common.util
 * @ClassName: SystemSecurityCertificationUtils
 *
 * @Author: xiaoma
 * @Date: 2022/1/12 18:26
 * @Version: v1.0
 **/
@UtilityClass
public class SystemSecurityCertificationUtils {

    /**
     * @Description: 系统变量配置
     * @Method: kerberosCertification
     * @Param: paramMap 1 参数列表
     * @Param: kerberosKeys 2 需要校验的参数列表
     * @Return: void
     *
     * @Author: xiaoma
     * @Date: 2022/1/13 11:30
     * @Version: V1.0
     */
    public void systemVariablesSetting(Map<String, ?> paramMap, List<String> kerberosKeys) {
        List<String> paramKeys = new ArrayList<>(paramMap.keySet());
        if (CollectionUtil.isNotEmpty(paramMap) && CollectionUtil.isNotEmpty(kerberosKeys)) {
            try {
                kerberosKeys.stream().forEach(kerberosKey -> paramKeys.stream().forEach(paramKey -> {
                    if (StringUtil.stringEqualsIgnoreCaseAndSymbol(paramKey, kerberosKey) && null != paramMap.get(paramKey)) {
                        System.setProperty(kerberosKey, paramMap.get(paramKey).toString());
                    }
                }));
                sun.security.krb5.Config.refresh();
                javax.security.auth.login.Configuration.getConfiguration().refresh();
            } catch (KrbException exception) {
                throw new SourceSystemException(SourceJdbcErrorCode.ERROR_CODE_00011, exception);
            }
        }
    }

    /**
     * @Description: 系统变量配置
     * @Method: settingSystemVariables
     * @Param: paramMap 参数列表
     * @Return: void
     *
     * @Author: xiaoma
     * @Date: 2022/1/17 11:35
     * @Version: V1.0
     */
    public void systemVariablesSetting(Map<String, String> paramMap) {
        if (CollectionUtil.isNotEmpty(paramMap)) {
            try {
                paramMap.entrySet().stream().forEach(t -> {
                    System.setProperty(t.getKey(), t.getValue());
                });
                sun.security.krb5.Config.refresh();
                javax.security.auth.login.Configuration.getConfiguration().refresh();
            } catch (KrbException exception) {
                throw new SourceSystemException(SourceJdbcErrorCode.ERROR_CODE_00011, exception);
            }
        }
    }

    public void systemUserLoginWithKeyTab(Configuration configuration, String user, String path) {
        try {
            UserGroupInformation.setConfiguration(configuration);
            UserGroupInformation.loginUserFromKeytab(user, path);
        } catch (IOException e) {
            throw new SourceSystemException(SourceJdbcErrorCode.ERROR_CODE_00012, e);
        }
    }

}
